﻿using System;
using System.Linq;
using DAL;
using Entity;
using Utils;
using Library;
using System.Collections.Generic;

namespace BLL
{
    public class CtrAdmin
    {
        /// <summary>
        /// Check exist userName
        /// </summary>
        /// <returns></returns>
        public bool CheckExistsAdminByUserName(string userName, int servicesId)
        {
            int? ret = 0;
            DB.AdminInstance.uspAdminCheckExistsByUserName(userName, servicesId, ref ret);
            if (ret != null) return (int)ret == 1;
            return false;
        }

        /// <summary>
        /// Check Login BackEnd
        /// </summary>
        /// <returns></returns>
        public bool CheckLogin(string userName, string passWord)
        {
            int? ret = 0;
            passWord = Lib.GetMd5(passWord);
            ret = DB.AdminInstance.uspAdminCheckLogin(userName, passWord);
            return ret > 0;
        }
        /// <summary>
        /// Change pass word
        /// </summary>
        /// <returns></returns>
        public int ChangePass(string passWordOld, string passWordNew)
        {
            if (!AdminUtils.IsLogin)
                return -1;
            passWordOld = Lib.GetMd5(passWordOld);
            passWordNew = Lib.GetMd5(passWordNew);
            return DB.AdminInstance.uspAdminChangePass(AdminUtils.AdminID, passWordOld, passWordNew);
        }

        /// <summary>
        /// Set mọi Session
        /// </summary>
        public static void Login(string userName, string permission, int serviceId)
        {
            var infoAdmin = DB.AdminInstance.uspAdminGetInfoByUserName(userName).ToList().FirstOrDefault();

            if (infoAdmin != null)
            {
                AdminUtils.AdminID = infoAdmin.Id;
                AdminUtils.UserName = infoAdmin.UserName;
                AdminUtils.FullName = infoAdmin.FullName;
            }
            AdminUtils.Permission = permission;
            AdminUtils.ServicesID = serviceId;

        }

        /// <summary>
        /// Kiểm tra admin đang đăng nhập có đươc quyền thực hiện chức năng truyền vào hay không?
        /// </summary>
        public static int GetPermission(string funtion)
        {
            var mPermission = 0;
            if (!String.IsNullOrEmpty(AdminUtils.Permission) && !funtion.Equals(string.Empty))
            {
                var permissionList = AdminUtils.Permission.Split('|');
                mPermission = (from permission in permissionList
                               select permission.Split('#')
                                   into arrTemp
                                   where arrTemp[0].ToLower().Equals(funtion.ToLower())
                                   select Convert.ToInt32(arrTemp[1])).FirstOrDefault();
            }
            return mPermission;
        }

        /// <summary>
        /// Xóa mọi Session
        /// </summary>
        public static void Logout()
        {
            AdminUtils.AdminID = -1;
            AdminUtils.UserName = string.Empty;
            AdminUtils.FullName = string.Empty;
            AdminUtils.Permission = string.Empty;
        }

        public uspAdminGetInfoByAdminIdResult GetInfoAdminByAdminId(int adminId)
        {
            return DB.AdminInstance.uspAdminGetInfoByAdminId(adminId).FirstOrDefault();
        }

        public uspAdminGetInfoByUserNameResult GetInfoAdminByUserName(string userName)
        {
            return DB.AdminInstance.uspAdminGetInfoByUserName(userName).ToList().FirstOrDefault();
        }

        public ClassExtend<int, uspAdminGetListResult> GetListAdmin(string userName, int adminId, int roleId, short status, DateTime fromDate, DateTime toDate, int currentPage, int pageSize)
        {
            var objReturn = new ClassExtend<int, uspAdminGetListResult>();
            int? totalRecord = 0;
            objReturn.Items = DB.AdminInstance.uspAdminGetList(userName, adminId, roleId, fromDate, toDate, status, currentPage, pageSize, ref totalRecord).ToList();
            if (totalRecord != null) objReturn.TotalRecord = totalRecord.Value;
            objReturn.Info = 0;
            return objReturn;
        }

        public ClassExtend<int, uspAdminGetListAllResult> GetListAdminAll()
        {
            var objReturn = new ClassExtend<int, uspAdminGetListAllResult> { Items = DB.AdminInstance.uspAdminGetListAll().ToList() };
            objReturn.TotalRecord = objReturn.Items.Count;
            objReturn.Info = 0;
            return objReturn;
        }


        public int InsertAdmin(string userName,string passWord, string fullName, string birthday, string YM, string phone, string description, short status)
        {
            fullName = HtmlUtility.HtmlEncode(fullName);
            description = HtmlUtility.HtmlEncode(description);
            YM = HtmlUtility.HtmlEncode(YM);
            phone = HtmlUtility.HtmlEncode(phone);
            passWord = Lib.GetMd5(passWord);
            if (birthday != "")
            {
                var dateTime = Lib.GetDateTime(birthday);
                if (dateTime != null)
                {
                    var birthdayDate = (DateTime)dateTime;
                    var result = DB.AdminInstance.uspAdminInsert(userName, passWord,fullName, birthdayDate, YM, phone, description, DateTime.Now, status);
                    if (result == -1) return -2;
                    return result;
                }
            }
            else
            {
                var result = DB.AdminInstance.uspAdminInsert(userName, passWord, fullName, null, YM, phone, description, DateTime.Now, status);
                if (result == -1) return -2;
                return result;
            }
            return -2;

        }

        /// <summary>
        /// Cập nhật thông tin cá nhân Admin (do admin đó tự cập nhật thông tin cá nhân của mình)
        /// </summary>
        /// <param name="fullName"></param>
        /// <param name="birthday"></param>
        /// <param name="ym"> </param>
        /// <param name="phone"> </param>
        /// <returns></returns>
        public int UpdateAdminInfo(string fullName, DateTime birthday, string ym, string phone)
        {
            fullName = HtmlUtility.HtmlEncode(fullName);
            ym = HtmlUtility.HtmlEncode(ym);
            phone = HtmlUtility.HtmlEncode(phone);
            return DB.AdminInstance.uspAdminUpdateInfo(AdminUtils.AdminID, fullName, birthday, ym, phone);
        }

        /// <summary>
        /// Admin cấp cao nhất cập nhật thông tin cua các user admin trong backend
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="status"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public int UpdateAdminStatus(int adminId, string description, short status)
        {
            description = HtmlUtility.HtmlEncode(description);
            return DB.AdminInstance.uspAdminUpdateStatus(adminId, description, status);
        }

        public int DeleteAdmin(int adminId)
        {
            return DB.AdminInstance.uspAdminDelete(adminId);
        }        
    }
}
